//
//  VideoSlide.swift
//  onemeetingmac
//
//  Created by 张衡 on 2020/9/30.
//  Copyright © 2020 onemeeting. All rights reserved.
//

import Foundation

class VideoSlide: NSView {
    
    var value:Double = 0
    var minValue:Double = 0
    var maxValue:Double = 0
    var sliderAction:((Int)->())?

    override init(frame frameRect: NSRect) {
        super.init(frame: frameRect)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    var currentValue:Int = 0 {
        didSet {
            self.slide.cell?.stringValue = String(currentValue)
            self.title.stringValue = String(currentValue)
        }
    }
    
    convenience init(value: Double, minValue: Double, maxValue: Double,action:((Int)->())?) {
        self.init()
        self.value = value
        self.minValue = minValue
        self.maxValue = maxValue
        self.sliderAction = action
        self.setupUI()
    }
    func setupUI() {
        self.addSubview(title)
        title.snp.makeConstraints { (make) in
            make.right.centerY.equalToSuperview()
            make.height.equalTo(20)
        }
        self.addSubview(slide)
        slide.snp.makeConstraints { (make) in
            make.left.equalToSuperview()
            make.right.equalTo(title.snp.left).offset(-10)
            make.centerY.equalToSuperview()
        }
    }
    
    lazy var slide: NSSlider = {
        let view = NSSlider.init(value: self.value, minValue: self.minValue, maxValue: self.maxValue, target: self, action: #selector(slideAction(senber:)))
        return view
    }()
    
    lazy var title: NSLabel = {
        let string = String.init(format: "%.0f", self.value)
        let lab = NSLabel.label(title: string, textColor: color_gray(0), font: NSFont.systemFont(ofSize: 14))
        lab.alignment = .right
        lab.sizeToFit()
        return lab
    }()
    
    @objc func slideAction(senber:NSSlider) {
        if let value = senber.cell?.integerValue {
            self.currentValue = value
            if let action = self.sliderAction {
                action(value)
            }
        }
        
    }
}
